/* eslint-disable no-nested-ternary */
import { ref, watch, defineComponent } from 'vue';
import {
  getDatePickerProps,
  getEditorEmits,
  useNamespace,
} from '@ibiz-template/vue3-util';
import './ibiz-date-picker.scss';
import dayjs from 'dayjs';
import { DatePickerEditorController } from '../date-picker-editor.controller';

export const IBizDatePicker = defineComponent({
  name: 'IBizDatePicker',
  props: getDatePickerProps<DatePickerEditorController>(),
  emits: getEditorEmits(),
  setup(props, { emit }) {
    const ns = useNamespace('date-picker');
    const c = props.controller;

    const editorModel = c!.model;

    const type = ref('date');
    const format = ref('YYYY-MM-DD');

    const isTimePicker = ref(false);

    switch (editorModel.editorType) {
      case 'DATEPICKEREX':
      case 'DATEPICKEREX_NOTIME':
        type.value = 'date';
        break;
      case 'DATEPICKEREX_NODAY':
      case 'DATEPICKEREX_NODAY_NOSECOND':
        isTimePicker.value = true;
        type.value = 'time';
        break;
      case 'DATEPICKEREX_HOUR':
      case 'DATEPICKEREX_MINUTE':
      case 'DATEPICKEREX_SECOND':
      case 'DATEPICKEREX_NOSECOND':
      case 'DATEPICKER':
      default:
        type.value = 'datetime';
    }
    // 值格式化
    const valueFormat = c!.valueFormat;
    if (valueFormat) {
      format.value = valueFormat;
    }

    // 格式化后的值
    const formatValue = ref();
    watch(
      () => props.value,
      (newVal, oldVal) => {
        // 空值不转换
        if (newVal && newVal !== oldVal) {
          const formatVal = dayjs(newVal).format(valueFormat);
          if (formatVal !== 'Invalid Date') {
            formatValue.value = formatVal;
          } else {
            formatValue.value = newVal;
          }
        }
      },
      { immediate: true },
    );
    // 处理值变更
    const handleChange = (date: string, _dateType: IData) => {
      emit('change', date);
    };

    const onOpenChange = (_isOpen: boolean) => {};

    const inputRef = ref();

    if (props.autoFocus) {
      watch(inputRef, newVal => {
        if (newVal) {
          const input = newVal.$el.getElementsByTagName('input')[0];
          input.click();
        }
      });
    }

    // 聚焦
    const onFocus = () => {
      emit('focus');
    };

    // 失焦
    const onBlur = () => {
      emit('blur');
    };

    return {
      ns,
      c,
      editorModel,
      type,
      format,
      formatValue,
      handleChange,
      onOpenChange,
      inputRef,
      isTimePicker,
      onFocus,
      onBlur,
    };
  },
  render() {
    return (
      <div
        class={[
          this.ns.b(),
          this.disabled ? this.ns.m('disabled') : '',
          this.readonly ? this.ns.m('readonly') : '',
        ]}
      >
        {this.readonly ? (
          this.formatValue
        ) : this.isTimePicker ? (
          <el-time-picker
            ref='inputRef'
            type={this.type}
            format={this.format}
            value-format={this.format}
            placeholder={this.c!.placeHolder}
            v-model={this.formatValue}
            onVisibleChange={this.onOpenChange}
            onChange={this.handleChange}
            disabled={this.disabled}
            onFocus={this.onFocus}
            onBlur={this.onBlur}
          ></el-time-picker>
        ) : (
          <el-date-picker
            ref='inputRef'
            type={this.type}
            format={this.format}
            value-format={this.format}
            placeholder={this.c!.placeHolder}
            v-model={this.formatValue}
            onVisibleChange={this.onOpenChange}
            onChange={this.handleChange}
            disabled={this.disabled}
            onFocus={this.onFocus}
            onBlur={this.onBlur}
          ></el-date-picker>
        )}
      </div>
    );
  },
});
